#include "ColorEvent.h"
#include "InterpolationProcessor.h"
#include "ConvertValueToString.h"

ColorEvent::ColorEvent(Ogre::Vector4 past,Ogre::Vector4 current){
	this->_past_value = ConvertValueToString::Vector4toString(past);
	this->_current_value = ConvertValueToString::Vector4toString(current);
	this->_name = "Color_Change";
	this->_color_variation = Ogre::StringConverter::parseVector4(this->_past_value);
}

void ColorEvent::writePastValueToVariation(){
	this->_variation = this->_past_value;
	this->_color_variation = Ogre::StringConverter::parseVector4(this->_past_value);
}
void ColorEvent::generateInterpolation(){
	InterpolationProcessor ipp;
	Ogre::Vector4 pastvalue = Ogre::StringConverter::parseVector4(this->_past_value);
	Ogre::Vector4 currentvalue = Ogre::StringConverter::parseVector4(this->_current_value);
	Ogre::Vector4 ipvalue = ipp.excution(pastvalue,currentvalue);
	this->_ipValue = ConvertValueToString::Vector4toString(ipvalue);
}

bool ColorEvent::checkInterpolationValueIsZero(){
	Ogre::Vector4 ipvalue = Ogre::StringConverter::parseVector4(this->_ipValue);
	if(ipvalue == Ogre::Vector4::ZERO){
		return true;
	}
	return false;
}

bool ColorEvent::checkBounding(){
	Ogre::Vector4 color_ipvalue = Ogre::StringConverter::parseVector4(this->_ipValue);
	if(color_ipvalue != Ogre::Vector4::ZERO){
		this->_color_variation+= color_ipvalue;
		this->_variation = ConvertValueToString::Vector4toString(_color_variation);
		Ogre::Vector4 unit = Ogre::Vector4(1.0,1.0,1.0,1.0);
		Ogre::Real variation = this->_color_variation.dotProduct(unit);
		Ogre::Real current = Ogre::StringConverter::parseVector4(this->_current_value).dotProduct(unit);
		Ogre::Real ip = color_ipvalue.dotProduct(unit);
		if(isOverflow(variation,current,ip)){
			this->_color_variation = Ogre::StringConverter::parseVector4(this->_past_value);
			this->_variation = ConvertValueToString::Vector4toString(_color_variation);
			return false;
		}
	}
	return true;
}

void ColorEvent::recover(){
	writePastValueToVariation();
	this->_ipValue = "0.0 0.0 0.0 0.0";
	this->_color_variation = Ogre::StringConverter::parseVector4(this->_past_value);
}